package com.hzkq.widgets.chart.simple;

import android.graphics.Paint.Align;
import android.os.Bundle;
import android.widget.Button;

import com.hzkq.widgets.R;
import com.keqiang.chart.impl.axis.Label;
import com.keqiang.chart.impl.axis.XAxis;
import com.keqiang.chart.impl.axis.YAxis;
import com.keqiang.chart.impl.axis.impl.bar.BarChartHighLightRender;
import com.keqiang.chart.impl.axis.impl.bar.BarData;
import com.keqiang.chart.impl.axis.impl.bar.BarEntity;
import com.keqiang.chart.impl.axis.impl.bar.BarGroup;
import com.keqiang.chart.impl.axis.impl.combined.ChartType;
import com.keqiang.chart.impl.axis.impl.combined.CombinedChart;
import com.keqiang.chart.impl.axis.impl.combined.CombinedChartHighLightRender;
import com.keqiang.chart.impl.axis.impl.combined.CombinedChartView;
import com.keqiang.chart.impl.axis.impl.combined.CombinedData;
import com.keqiang.chart.impl.axis.impl.combined.YAxisType;
import com.keqiang.chart.impl.axis.impl.line.Gravity;
import com.keqiang.chart.impl.axis.impl.line.LineChartHighLightRender;
import com.keqiang.chart.impl.axis.impl.line.LineData;
import com.keqiang.chart.impl.axis.impl.line.LineEntity;
import com.keqiang.chart.impl.axis.impl.line.LineStyle;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import me.zhouzhuo810.magpiex.ui.act.BaseActivity;
import me.zhouzhuo810.magpiex.ui.widget.TitleBar;
import me.zhouzhuo810.magpiex.utils.SimpleUtil;

/**
 * 组合图表
 *
 * @author Created by wanggaowan on 2022/7/26 15:00
 */
public class CombinedChartActivity extends BaseActivity {
    
    private TitleBar mTitleBar;
    private CombinedChart mCombinedChart;
    private CombinedChart mCombinedChart2;
    private CombinedChart mCombinedChart3;
    private Button mBtRefresh;
    private Button mBtRefresh2;
    
    @Override
    public int getLayoutId() {
        return R.layout.activity_simple_combined_chart;
    }
    
    @Override
    public boolean shouldSupportMultiLanguage() {
        return false;
    }
    
    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        mTitleBar = findViewById(R.id.title_bar);
        CombinedChartView combinedChartView = findViewById(R.id.combined_chart);
        mCombinedChart = combinedChartView.getChart();
        mBtRefresh = findViewById(R.id.bt_refresh);
        CombinedChartView combinedChartView2 = findViewById(R.id.combined_chart2);
        mCombinedChart2 = combinedChartView2.getChart();
        mBtRefresh2 = findViewById(R.id.bt_refresh2);
        CombinedChartView combinedChartView3 = findViewById(R.id.combined_chart3);
        mCombinedChart3 = combinedChartView3.getChart();
        
        initLineChart1();
        initLineChart2();
        initLineChart3();
    }
    
    @Override
    public void initData() {
        fillLineChart1Data();
        fillLineChart2Data();
        fillLineChart3Data();
    }
    
    @Override
    public void initEvent() {
        mTitleBar.getLlLeft().setOnClickListener(v -> closeAct());
        
        mBtRefresh.setOnClickListener(v -> fillLineChart1Data());
        
        mBtRefresh2.setOnClickListener(v -> fillLineChart2Data());
    }
    
    private void initLineChart1() {
        XAxis xAxis = mCombinedChart.getXAxis();
        xAxis.setDrawGridLine(false);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(6);
        xAxis.setLabelMaxLines(2);
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        xAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        
        YAxis yAxis = mCombinedChart.getYAxis();
        yAxis.setDrawGridLine(true);
        yAxis.setGridUseEndPadding(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(11);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        yAxis.setMin(0);
        yAxis.setMax(200);
        
        xAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mCombinedChart.setHighLightRender(new CombinedChartHighLightRender(
            new LineChartHighLightRender(mCombinedChart.getLineChar()),
            new BarChartHighLightRender(mCombinedChart.getBarChar())
        ));
    }
    
    private void initLineChart2() {
        mCombinedChart2.setMainChart(ChartType.LINE);
        XAxis xAxis = mCombinedChart2.getXAxis();
        xAxis.setDrawGridLine(false);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(7);
        xAxis.setLabelMaxLines(2);
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        xAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        
        YAxis yAxis = mCombinedChart2.getYAxis();
        yAxis.setDrawGridLine(true);
        yAxis.setGridUseEndPadding(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(11);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        yAxis.setMin(0);
        yAxis.setMax(200);
        
        xAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mCombinedChart2.setHighLightRender(new CombinedChartHighLightRender(
            new LineChartHighLightRender(mCombinedChart2.getLineChar()),
            new BarChartHighLightRender(mCombinedChart2.getBarChar())
        ));
    }
    
    private void initLineChart3() {
        mCombinedChart3.setMainChart(ChartType.BAR);
        XAxis xAxis = mCombinedChart3.getXAxis();
        xAxis.setDrawGridLine(false);
        xAxis.setGridUseEndPadding(true);
        xAxis.setLabelAlign(Align.CENTER);
        xAxis.setLabelOverAlign(Align.LEFT);
        xAxis.setLabelCount(6);
        xAxis.setLabelMaxLines(2);
        xAxis.setEndPadding(0);
        xAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        
        YAxis yAxis = mCombinedChart3.getYAxis();
        yAxis.setDrawGridLine(true);
        yAxis.setLabelAlign(Align.RIGHT);
        yAxis.setLabelMaxLines(2);
        yAxis.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxis.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxis.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxis.setLabelCount(11);
        yAxis.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        yAxis.setMin(0);
        yAxis.setMax(200);
        
        YAxis yAxisEnd = new YAxis();
        yAxisEnd.setDrawGridLine(false);
        yAxisEnd.setLabelAlign(Align.LEFT);
        yAxisEnd.setLabelMaxLines(2);
        yAxisEnd.setLabelOffsetAxis(SimpleUtil.getScaledValue(10));
        yAxisEnd.setEndPadding(SimpleUtil.getScaledValue(30));
        yAxisEnd.setLabelMaxWidth(SimpleUtil.getScaledValue(100f));
        yAxisEnd.setLabelCount(11);
        yAxisEnd.setLabelMinWidth(SimpleUtil.getScaledValue(60));
        yAxisEnd.setMin(0);
        yAxisEnd.setMax(100);
        mCombinedChart3.setYAxisEnd(yAxisEnd);
        
        xAxis.setAxisValueFormat(integer -> Label.obtain((integer + 1) + "月"));
        
        mCombinedChart3.setHighLightRender(new CombinedChartHighLightRender(
            new LineChartHighLightRender(mCombinedChart3.getLineChar()),
            new BarChartHighLightRender(mCombinedChart3.getBarChar())
        ));
    }
    
    /**
     * 设置图表数据
     */
    private void fillLineChart1Data() {
        List<BarGroup> barDataList = new ArrayList<>();
        List<LineData> lineDataList = new ArrayList<>();
        List<LineEntity> lineEntityList = new ArrayList<>();
        
        YAxis yAxis = mCombinedChart.getYAxis();
        int count = mCombinedChart.getXAxis().getLabelCount();
        LineEntity max = null;
        
        for (int i = 0; i < count; i++) {
            float avg2 = 0;
            // 柱状图数据
            List<BarEntity> entityList = new ArrayList<>();
            for (int j = 0; j < 3; j++) {
                BarEntity entity = new BarEntity((float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
                avg2 += entity.getValue();
                entity.setDrawLabel(true);
                entity.setLabel(((int) entity.getValue()) + "");
                if (j == 0) {
                    entity.setWidthPercent(0.4f);
                    entity.setColor(0xff61F2AD);
                } else if (j == 1) {
                    entity.setWidthPercent(0.2f);
                    entity.setColor(0xffA2E4FC);
                } else {
                    entity.setWidthPercent(0.2f);
                    entity.setColor(0xffFF6600);
                }
                entityList.add(entity);
            }
            BarGroup barData = new BarGroup(entityList);
            barData.setBarOffset(SimpleUtil.getScaledValue(5));
            barDataList.add(barData);
            
            // 折线图数据
            LineEntity entity = new LineEntity(i + 0.6f, avg2 / 3f);
            if (i == 2) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            } else if (i == 4) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            }
            
            if (i == 0) {
                entity.setLabelGravity(Gravity.START);
            } else if (i == count - 1) {
                entity.setLabelGravity(Gravity.END);
            } else {
                entity.setLabelGravity(Gravity.CENTER);
            }
            
            entity.setLabelOffset(10);
            entity.setIndicatorDrawable(ContextCompat.getDrawable(this, R.drawable.bg_circle_blue));
            entity.setDrawLabel(false);
            entity.setDrawIndicator(true);
            entity.setLabel(((int) entity.getValue()) + "");
            lineEntityList.add(entity);
            
            if (max == null || max.getValue() < entity.getValue()) {
                max = entity;
            }
        }
        
        if (max != null) {
            max.setDrawLabel(true);
        }
        
        LineData lineData = new LineData(lineEntityList);
        lineData.setLineWidth(SimpleUtil.getScaledValue(2));
        lineData.setLineStyle(LineData.CUBIC_BEZIER);
        lineData.setFillBg(false);
        lineData.setFillBgColor(0x335283C9);
        lineData.setLineColor(0xFF106CF5);
        lineDataList.add(lineData);
        
        CombinedData data = new CombinedData();
        data.setLineDataList(lineDataList);
        BarData barData = new BarData();
        barData.setGroups(barDataList);
        data.setBarData(barData);
        
        mCombinedChart.setData(data);
        mCombinedChart.animDraw(2000, ChartType.LINE);
    }
    
    /**
     * 设置图表数据
     */
    private void fillLineChart2Data() {
        List<BarGroup> barDataList = new ArrayList<>();
        List<LineData> lineDataList = new ArrayList<>();
        List<LineEntity> lineEntityList = new ArrayList<>();
        
        YAxis yAxis = mCombinedChart2.getYAxis();
        int count = mCombinedChart2.getXAxis().getLabelCount();
        LineEntity max = null;
        
        for (int i = 0; i < count; i++) {
            float avg2 = 0;
            if (i < 6) {
                // 柱状图数据
                List<BarEntity> entityList = new ArrayList<>();
                for (int j = 0; j < 3; j++) {
                    BarEntity entity = new BarEntity((float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
                    avg2 += entity.getValue();
                    entity.setDrawLabel(true);
                    if (j == 0) {
                        entity.setWidthPercent(0.4f);
                        entity.setColor(0xff61F2AD);
                    } else if (j == 1) {
                        entity.setWidthPercent(0.2f);
                        entity.setColor(0xffA2E4FC);
                    } else {
                        entity.setWidthPercent(0.2f);
                        entity.setColor(0xffFF6600);
                    }
                    entityList.add(entity);
                }
                BarGroup barData = new BarGroup(entityList);
                barData.setBarOffset(SimpleUtil.getScaledValue(5));
                barDataList.add(barData);
            }
            
            // 折线图数据
            LineEntity entity =
                new LineEntity(i, i < 6 ? avg2 / 3f : (float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
            if (i == 2) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            } else if (i == 4) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            }
            
            if (i == 0) {
                entity.setLabelGravity(Gravity.START);
            } else if (i == count - 1) {
                entity.setLabelGravity(Gravity.END);
            } else {
                entity.setLabelGravity(Gravity.CENTER);
            }
            
            entity.setLabelOffset(10);
            entity.setIndicatorDrawable(ContextCompat.getDrawable(this, R.drawable.bg_circle_blue));
            entity.setDrawLabel(false);
            entity.setDrawIndicator(true);
            entity.setLabel(((int) entity.getValue()) + "%");
            lineEntityList.add(entity);
            
            if (max == null || max.getValue() < entity.getValue()) {
                max = entity;
            }
        }
        
        if (max != null) {
            max.setDrawLabel(true);
        }
        
        LineData lineData = new LineData(lineEntityList);
        lineData.setLineWidth(SimpleUtil.getScaledValue(2));
        lineData.setLineStyle(LineData.CUBIC_BEZIER);
        lineData.setFillBg(false);
        lineData.setFillBgColor(0x335283C9);
        lineData.setLineColor(0xFF106CF5);
        lineDataList.add(lineData);
        
        CombinedData data = new CombinedData();
        data.setLineDataList(lineDataList);
        BarData barData = new BarData();
        barData.setGroups(barDataList);
        data.setBarData(barData);
        
        mCombinedChart2.setData(data);
        mCombinedChart2.animDraw(2000, ChartType.LINE);
    }
    
    /**
     * 设置图表数据
     */
    private void fillLineChart3Data() {
        YAxis yAxisEnd = mCombinedChart3.getYAxisEnd();
        if (yAxisEnd == null) {
            return;
        }
        
        List<BarGroup> barDataList = new ArrayList<>();
        List<LineData> lineDataList = new ArrayList<>();
        List<LineEntity> lineEntityList = new ArrayList<>();
        
        YAxis yAxis = mCombinedChart3.getYAxis();
        int count = mCombinedChart3.getXAxis().getLabelCount() + 1;
        LineEntity max = null;
        
        for (int i = 0; i < count; i++) {
            if (i < 6) {
                // 柱状图数据
                List<BarEntity> entityList = new ArrayList<>();
                BarEntity entity = new BarEntity((float) (yAxis.getMin() + Math.random() * (yAxis.getMax() - yAxis.getMin())));
                entity.setDrawLabel(true);
                entity.setWidthPercent(0.8f);
                entity.setColor(0xff61F2AD);
                entityList.add(entity);
                BarGroup barData = new BarGroup(entityList);
                barData.setBarOffset(SimpleUtil.getScaledValue(5));
                barDataList.add(barData);
            }
            
            // 折线图数据
            LineEntity entity = new LineEntity(i, (float) (yAxisEnd.getMin() + Math.random() * (yAxisEnd.getMax() - yAxisEnd.getMin())));
            if (i == 2) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            } else if (i == 4) {
                entity.setLineStyle(LineStyle.DOTTED_LINE);
            }
            
            if (i == 0) {
                entity.setLabelGravity(Gravity.START);
            } else if (i == count - 1) {
                entity.setLabelGravity(Gravity.END);
            } else {
                entity.setLabelGravity(Gravity.CENTER);
            }
            
            entity.setLabelOffset(10);
            entity.setIndicatorDrawable(ContextCompat.getDrawable(this, R.drawable.bg_circle_blue));
            entity.setDrawLabel(false);
            entity.setDrawIndicator(true);
            entity.setLabel(((int) entity.getValue()) + "%");
            lineEntityList.add(entity);
            
            if (max == null || max.getValue() < entity.getValue()) {
                max = entity;
            }
        }
        
        if (max != null) {
            max.setDrawLabel(true);
        }
        
        LineData lineData = new LineData(lineEntityList);
        lineData.setLineWidth(SimpleUtil.getScaledValue(2));
        lineData.setLineStyle(LineData.CUBIC_BEZIER);
        lineData.setFillBg(false);
        lineData.setFillBgColor(0x335283C9);
        lineData.setLineColor(0xFF106CF5);
        lineDataList.add(lineData);
        
        CombinedData data = new CombinedData();
        data.setLineDataList(lineDataList, YAxisType.END);
        BarData barData = new BarData();
        barData.setGroups(barDataList);
        data.setBarData(barData);
        
        mCombinedChart3.setData(data);
        mCombinedChart3.animDraw(2000, ChartType.LINE);
    }
}
